﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.RestrictedUsage.CSharp.Semantics;
using System.Reflection;

namespace CSharpParser.SCharpType
{
    
    public static class Helper
    {


        public static object[] GetCustomAttributes(CSharpType type, Type attributeType, bool inherit)
        {

            List<Object> l = new List<object>();

            foreach (CSharpCustomAttribute item in type.Attributes)
            {

                if (item.Constructor == null)
                    continue;

                CSharpType t = item.Constructor.ContainingType;

                if (t == null)
                    continue;

                string name1 = t.FullNamespace() + "." + t.Name.Text;
                string name2 = attributeType.FullName;

                if (name1 == name2)
                    l.Add(item);

                if (inherit && type.BaseClass != null)
                    l.AddRange(GetCustomAttributes(type.BaseClass, attributeType, inherit));

            }

            return l.ToArray();
        }


        public static object[] GetCustomAttributes(CSharpMember item, Type attributeType, bool inherit)
        {

            List<Object> l = new List<object>();

            foreach (CSharpCustomAttribute attr in item.Attributes)
            {

                if (attr.Constructor == null)
                    continue;

                CSharpType t = attr.Constructor.ContainingType;

                if (t == null)
                    continue;

                string name1 = t.FullNamespace() + "." + t.Name.Text;
                string name2 = attributeType.FullName;

                if (name1 == name2)
                    l.Add(attr);

                if (inherit && item.ContainingType.BaseClass != null)
                    l.AddRange(GetCustomAttributes(item.ContainingType.BaseClass, attributeType, inherit));

            }

            return l.ToArray();
        }


        public static bool IsDefined(CSharpType type, Type attributeType, bool inherit)
        {

            List<Object> l = new List<object>();

            foreach (CSharpCustomAttribute item in type.Attributes)
            {

                if (item.Constructor == null)
                    continue;

                CSharpType t = item.Constructor.ContainingType;

                if (t == null)
                    continue;

                string name1 = t.FullNamespace() + "." + t.Name.Text;
                string name2 = attributeType.FullName;

                if (name1 == name2)
                    return true;

                if (inherit && type.BaseClass != null)
                    if (IsDefined(type.BaseClass, attributeType, inherit))
                        return true;
            }

            return false;
        }


        public static bool IsDefined(CSharpMember item, Type attributeType, bool inherit)
        {

            List<Object> l = new List<object>();

            foreach (CSharpCustomAttribute attr in item.Attributes)
            {

                if (attr.Constructor == null)
                    continue;

                CSharpType t = attr.Constructor.ContainingType;

                if (t == null)
                    continue;

                string name1 = t.FullNamespace() + "." + t.Name.Text;
                string name2 = attributeType.FullName;

                if (name1 == name2)
                    return true;

                if (inherit && item.ContainingType.BaseClass != null)
                    if (IsDefined(item.ContainingType.BaseClass, attributeType, inherit))
                        return true;
            }

            return false;
        }


        public static string FullNamespace(this CSharpType item)
        {
            return FullNamespace(item.ContainingNamespace);
        }

        private static string FullNamespace(CSharpNamespace item)
        {
            string ret = item.Name.Text;

            if (item.ContainingNamespace != null && !string.IsNullOrEmpty(item.ContainingNamespace.Name.Text))
                ret = FullNamespace(item.ContainingNamespace) + "." + ret;

            return ret;
        }

        public static AssemblyName GetAssemblyName(this Microsoft.RestrictedUsage.CSharp.Semantics.CSharpAssembly ass)
        {

            AssemblyName l = AssemblyName.GetAssemblyName(ass.BinaryFileName.Value);

            return l;
           

        }



    }

}
